//
//  SpreadViewController.m
//  HelpThePoor
//
//  Created by van7ish on 2016/11/25.
//  Copyright © 2016年 van7ish. All rights reserved.
//

#import "SpreadViewController.h"
#import "WebViewDetailVC.h"

@interface SpreadViewController () <UIWebViewDelegate>

/** 导航条 */
@property (nonatomic, strong) UIImageView * navBar;
/** 推广信息页面 */
@property (nonatomic, strong) UIImageView * imageView;
//@property (nonatomic, strong) UIWebView * webView;
@property (nonatomic, strong) UIImageView * qrCodeImageView;


@end

@implementation SpreadViewController

- (UIImageView *)qrCodeImageView{
    if (!_qrCodeImageView) {
        _qrCodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_W - (SCREEN_H - 120) * 0.172 * 0.854) / 2, CGRectGetMaxY(self.imageView.frame), (SCREEN_H - 120) * 0.172 * 0.854, (SCREEN_H - 120) * 0.172)];
        _qrCodeImageView.image = ImageNamed(@"qrcode");
        _qrCodeImageView.userInteractionEnabled = YES;
        
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapQrCode:)];
        longPress.minimumPressDuration = 1.0;
        [_qrCodeImageView addGestureRecognizer:longPress];
    }
    return _qrCodeImageView;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_W - (SCREEN_H - 120) * 0.828 * 0.74) / 2, 64, (SCREEN_H - 120) * 0.828 * 0.74, (SCREEN_H - 120) * 0.828)];
        _imageView.image = ImageNamed(@"tuiguanggongcheng");

    }
    return _imageView;
}

//- (UIWebView *)webView{
//    if (!_webView) {
//        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_W, SCREEN_H - 64 - 49)];
//        _webView.scrollView.backgroundColor = COLOR_LIGHTBGCOLOR;
//        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#fffae7'"];
//        _webView.delegate = self;
//    }
//    return _webView;
//}

- (UIImageView *)navBar{
    if (!_navBar) {
        _navBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 64)];
        _navBar.image = ImageNamed(@"daohangtiao");
    }
    return _navBar;
}

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_LIGHTBGCOLOR;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.navBar];
    [self.view addSubview:self.imageView];
    [self.view addSubview:self.qrCodeImageView];
    
//    [self.view addSubview:self.webView];
//    [self requestData];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"loginSuccess" object:nil];
//    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"logoutSuccess" object:nil];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:0.1 animations:^{
        [AppDelegate shareDelegate].tabBar.customBar.center = CGPointMake(SCREEN_W / 2, SCREEN_H - [AppDelegate shareDelegate].tabBar.customBar.frame.size.height / 2);
    }];
    
    [Tool setNavigationBarType:self];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)tapQrCode:(UILongPressGestureRecognizer *)longPress{
    
    
    if (longPress.state != UIGestureRecognizerStateBegan)
    {
        return;
    } else {
     
    }
        
    UIImageView*tempImageView=(UIImageView*)longPress.view;
    if(tempImageView.image){
        //1. 初始化扫描仪，设置设别类型和识别质量
        CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
        //2. 扫描获取的特征组
        NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:tempImageView.image.CGImage]];
        //3. 获取扫描结果
        CIQRCodeFeature *feature = [features objectAtIndex:0];
        NSString *scannedResult = feature.messageString;
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否查看数字图书馆详情页" message:scannedResult preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            DLog(@"打开链接");
            WebViewDetailVC *webVC = [[WebViewDetailVC alloc]init];
            webVC.urlStr = scannedResult;
            [self.navigationController pushViewController:webVC animated:NO];
        }];
        [alert addAction:action1];
        [alert addAction:action2];
        [self presentViewController:alert animated:YES completion:nil];
    }else {
        UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:@"您还没有生成二维码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

//#pragma mark - UIWebViewDelegate Method
//
//-(void)webViewDidFinishLoad:(UIWebView *)webView
//{
//    //页面背景色
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#fffae7'"];
//}

//#pragma mark - data request
//
//- (void)requestData{
//    NSString *URL = [NSString stringWithFormat:KSpreadURL, KHostAddress, KOrganizationID];
//    [ZRRequestManager POST:URL parameters:nil success:^(id responseObject) {
//        DLog(@"获取推广资源成功 %@", responseObject);
//        
//        [self.webView loadHTMLString:responseObject[@"data"] baseURL:nil];
//
//    } failure:^(NSError *error) {
//        DLog(@"获取推广资源失败%@", error);
//    }];
//}

@end
